/*
 *		ntspwd.cpp - Source for extension manager password override program
 */
//

	/* "C" header files */
#include "string.h"

	/* Platform header files */
#include "windows.h"
#include "global.h"
#include "nsferr.h"
#include "bsafeerr.h"
#include "extmgr.h"
LPVOID lpMapAddress;
VOID GetSharedMem(LPTSTR lpszBuf, DWORD cchSize) 
{ 
    LPTSTR lpszTmp; 
 
    // Get the address of the shared memory block.
 
    lpszTmp = (LPTSTR) lpMapAddress; 
 
    // Copy from shared memory into the caller's buffer.
 
    while (*lpszTmp && --cchSize) 
        *lpszBuf++ = *lpszTmp++; 
    *lpszBuf = '\0'; 
} 

// External routine. called to create and initialize shared memory.
BOOL GetNotesPassword (LPSTR pszPassword, int iMaxLen)
{
	BOOL fSuccess = FALSE;

//	Assert( iMaxLen < 256 );
	HANDLE hMapFile;
	CHAR szTemp[1024];
	//DebugBreak();

	hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission. 
    FALSE,                             // Do not inherit the name
    "STSNotesPwd");            // of the mapping object. 
 
	if (hMapFile ) 
		{ 
 
		lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. 
			FILE_MAP_ALL_ACCESS,               // Read/write permission. 
			0,                                 // Max. object size. 
			0,                                 // Size of hFile. 
			0);                                // Map entire file. 
 
		if (lpMapAddress ) 
			{ 
				GetSharedMem(szTemp,iMaxLen);
				UnmapViewOfFile(lpMapAddress);
				CloseHandle(hMapFile); 
				strncpy_s( pszPassword, iMaxLen-1, szTemp, iMaxLen - 1 );
				pszPassword[ iMaxLen - 1 ] = '\0';

				fSuccess = TRUE;
			}
		} 

	return (fSuccess);
}




HEMREGISTRATION hHandler = 0;
	/* Master callback handler */
STATUS LNCALLBACK ExtHandler (
	EMRECORD far *	pRecord
) {
	VARARG_PTR		pArgs;

 		/* Function arguments */
	DWORD			MaxPwdLen;
	DWORD far *		retLength;
	char far *		retPassword;
	char far *		FileName;
	char far *		OwnerName;
	DWORD			DataLen;
	BYTE far *		Data;

	if (EM_GETPASSWORD != pRecord->EId)
		return (ERR_EM_CONTINUE);

		/* Fetch the arguments */
	        pArgs = pRecord->Ap;
	    MaxPwdLen = VARARG_GET (pArgs, DWORD);
	    retLength = VARARG_GET (pArgs, DWORD far *);
	  retPassword = VARARG_GET (pArgs, char far *);
	     FileName = VARARG_GET (pArgs, char far *);
	    OwnerName = VARARG_GET (pArgs, char far *);
	      DataLen = VARARG_GET (pArgs, DWORD);
	         Data = VARARG_GET (pArgs, BYTE far *);

		/* Use the current password */
	if ((NULL != retLength)
		&& (NULL != retPassword))
	{
		if (GetNotesPassword( retPassword, MaxPwdLen ))
		{
			*retLength = strlen( retPassword );
			return (ERR_BSAFE_EXTERNAL_PASSWORD);
		}
		else
			return (ERR_BSAFE_USER_ABORT);
	}

	return (ERR_EM_CONTINUE);
}

/*
 *		Architecture
 *		------------
 *
 *	An Extension Manager DLL contains the following major components:
 *
 *		1)  DLL startup and shutdown (in platform DLL file, e.g., extwin.c):
 *			Under Win32:	the -DllEntryPoint function, here DllMain
 *
 *		2)	Notes entry point at ordinal 1:  MainEntryPoint
 *
 *		3)	Extension manager callback:  ExtHandler
 */

	/* Extension manager entry point */
STDAPIV_(STATUS) MainEntryPoint (
	void
) {
	STATUS		status;
	char		msgBuf [256];

	status = EMRegister (
		EM_GETPASSWORD,
		EM_REG_BEFORE,
		ExtHandler,
		0,
		&hHandler);

	if (NOERROR != status)
	{
		wsprintf (msgBuf, "Could not register extension handler - status: 0x%lX",
			status);
		MessageBox (GetFocus (), msgBuf, "ExtMgr Password", MB_OK);
	}

	return (status);
}



	/* Deregister extensions */
STATUS LNPUBLIC ExtClear (
	void
) {
	STATUS		status;

	if (0 != hHandler)
		status = EMDeregister (hHandler);
	else
		status = NOERROR;

	return (status);
}




	/* Win32 DLL Startup/Shutdown routine */
STDAPIV_(BOOL) LibMain (
	HANDLE		hInst,
	ULONG		dwReason,
	LPVOID		lpReserved
) {

	switch (dwReason)
	{
		case DLL_PROCESS_ATTACH:
				/* Save the instance handle */
			break;

		case DLL_PROCESS_DETACH:
				/* Deregister extension manager callbacks */
			ExtClear ();
			break;
	}

	return (TRUE);
}



